package com.yiguo.logistics.app;

import com.yiguo.common.Dialogs;
import com.yiguo.common.PubConst;
import com.yiguo.logistics.app.R;
import com.yiguo.logistics.web.LoaderOrderDetails;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class ActInputcode extends Activity
{
   private EditText edtInputCode;
   private Button btnInputCodeOK;
   private Button btnInputCodeCancel;

   String searchResult;
   Dialogs dialogs;
//   LoaderOrderDetails loader;

   void InitControls()
   {
	  edtInputCode = (EditText) findViewById(R.id.edtInputCode);
	  btnInputCodeOK = (Button) findViewById(R.id.btnInputCodeOK);
	  btnInputCodeCancel = (Button) findViewById(R.id.btnInputCodeCancel);

   }

   void InitControlsListener()
   {
	  btnInputCodeOK.setOnClickListener(new OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			// TODO Auto-generated method stub

			final String barCode = edtInputCode.getText().toString().trim();
			String url = PubConst.Url.GetOrderBySerialNumber;
			url = String.format(url, barCode);
			Intent intent = new Intent();
			intent.setClass(ActInputcode.this, ActOrderDetails.class);

			LoaderOrderDetails loader = new LoaderOrderDetails(url, dialogs, barCode, intent, ActInputcode.this, PubConst.ActivityCode.INPUT_CODE);
			loader.execute((Void) null);
		 }
	  });

	  btnInputCodeCancel.setOnClickListener(new OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			// TODO Auto-generated method stub
			finish();
		 }
	  });

   }

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.act_inputcode);
	  dialogs = new Dialogs(ActInputcode.this);

	  InitControls();

	  InitControlsListener();

	  new Handler().postDelayed(new Runnable()// 界面进入时就自动打开输入法
			{
			   @Override
			   public void run()
			   {
				  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				  imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
			   }
			}, 300);

   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data)
   {
	  // TODO Auto-generated method stub
	  switch (requestCode)
	  {
		 case PubConst.ActivityCode.INPUT_CODE:
			setResult(RESULT_OK, data);
			finish();
			break;
	  }
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
	  { // 按下的如果是BACK，同时没有重复
//		  if (loader != null)
//		  {
//			 loader.cancel(true);
//		  }
		 setResult(RESULT_CANCELED);
		 finish();
	  }
	  return false;
   }
}
